package com.qcby.controller;


import com.qcby.entity.TCategory;
import com.qcby.form.TCategoryListForm;
import com.qcby.form.TOrderListForm;
import com.qcby.service.TCategoryService;
import com.qcby.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2025-02-04
 */
@RestController
@RequestMapping("/tCategory")
public class TCategoryController {
    @Autowired
    private TCategoryService categoryService;
    /**
     * 查询商品分类
     */
    @GetMapping("/list")
    public Result list(TCategoryListForm categoryListForm){
        Map<String,Object> map = new HashMap<>();
        map.put("pageList",this.categoryService.categoryList(categoryListForm));
        return Result.ok().put("data",map);
    }

    /**
     * 添加商品分类
     */
    @PostMapping("/add")
    public Result add(@RequestBody TCategory category){
        boolean save = this.categoryService.save(category);
        if(!save) return Result.error("商品类别添加失败");
        return Result.ok();
    }

    /**
     * 回显商品类别
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Integer id){
            Map map = new HashMap();
            TCategory category = this.categoryService.getById(id);
            map.put("categoryInfo", category);
            return Result.ok().put("data", map);

    }

    /**
     * 编辑商品类别
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody TCategory category){
        boolean updateById = this.categoryService.updateById(category);
        if(!updateById) return Result.error("编辑商品类别失败");
        return Result.ok();
    }
    /**
     * 删除商品类别
     */
    @DeleteMapping("/del")
    public Result del(@RequestBody Integer[] ids){
        boolean removeByIds = this.categoryService.removeByIds(Arrays.asList(ids));
        if(!removeByIds) return Result.error("删除商品类别失败");
        return Result.ok();
    }
}

